package com.wcy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wcy.entity.Permission;
import com.wcy.entity.SrMenu;
import com.wcy.mapper.PermissionMapper;
import com.wcy.mapper.SrMenuMapper;
import com.wcy.service.ISrMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 吴传亿
 * @since 2025-04-29
 */
@Service
public class SrMenuServiceImpl extends ServiceImpl<SrMenuMapper, SrMenu> implements ISrMenuService {
    @Autowired
    private SrMenuMapper srMenuMapper;
    @Autowired
    private PermissionMapper permissionMapper;


    @Override
    public List<SrMenu> getUserMenuList(HttpServletRequest request) {
        String header = request.getHeader("X-Perms");
        if(header != null&&!header.isEmpty()){
            String[] split = header.split(",");
            List<Permission> perms = permissionMapper.selectList(new QueryWrapper<Permission>().in("permission_name",Arrays.stream(split).collect(Collectors.toList())));
            List<Integer> permIds = perms.stream().filter(e->!"DISABLED".equals(e.getPermissionName())).map(Permission::getPermissionId).collect(Collectors.toList());
            return srMenuMapper.selectList(new QueryWrapper<SrMenu>().in("perm_id",permIds));
        }
        return new ArrayList<>();
    }
}
